﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 自定义消息
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CustomMessageController : ControllerBase
    {
        /// <summary>
        ///  增加 消息发送
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(CustomMessageRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                Othermessage info = new Othermessage();
                info.Title = obj.Title;
                info.Content = obj.Content;
                info.SendUserId = obj.SendUserId;
                info.Status = obj.Status ?? 0;
                info.TargetUserId = obj.TargetUserId;
                info.AddTime = DateTime.Now;
                info.DepId = obj.DepId;
                var data = OtherMessageBLL.Instance.Add(info);
                if (data.FirstParam != null)
                {
                    ControllerHelper.SendCustomMessage(data.FirstParam, info.TargetUserId.Value);
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/CustomMessage/Add Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  增加多个消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase AddArry(CustomMessageArrayRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                if (obj != null && obj.List != null)
                {
                    List<Othermessage> infos = new List<Othermessage>();
                    foreach (var item in obj.List)
                    {
                        Othermessage info = new Othermessage();
                        info.Title = item.Title;
                        info.Content = item.Content;
                        info.SendUserId = item.SendUserId;
                        info.Status = item.Status ?? 0;
                        info.TargetUserId = item.TargetUserId;
                        info.AddTime = DateTime.Now;
                        info.DepId = item.DepId;
                        infos.Add(info);
                    }
                    var data = OtherMessageBLL.Instance.Add(infos);
                    foreach (var item in data.FirstParam)
                    {
                        ControllerHelper.SendCustomMessage(item,item.TargetUserId.Value);
                    }
                }
                else
                {
                    result.Status = 0;
                    result.Message = "CustomMessage Array Is Null";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/CustomMessage/AddArry Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  更改 消息已读状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase ChangeStatus(ChangeMessageStatusRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                var data = OtherMessageBLL.Instance.ChangeMessageStatus(obj.MessageId,obj.Status);
                if (data.FirstParam != null)
                {
                    ControllerHelper.SendCustomMessage(data.FirstParam, data.FirstParam.SendUserId);
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/CustomMessage/ChangeStatus Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  移除消息发送记录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Remove(RemoveOtherMessageRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                result.Status = OtherMessageBLL.Instance.Remove(obj.MessageId);
             
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/CustomMessage/Remove Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  获取消息记录 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> GetByPage(ChangeMessageSearchRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                var page = Pickout.SetPage(obj);
                result.FirstParam=OtherMessageBLL.Instance.GetByPage(obj.MessageId,obj.SendUserId, obj.TargetUserId, obj.Status, page);
                result.SecondParam = page.TotalRows;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/CustomMessage/GetByPage Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }
    }
}